#!C:\Python26

from pluginManage import Plugin
import re
import os
import inspect
import sys
import WCWV2					# For Runtime importing of classes, it HAS to import itself
from webFunctions import isList
import WCWV2Lists

'''

Note:
Plugin loads webpage specific classes at runtime. It's incredibly hackish, but it does work, and makes extending the page functions much easier.
Python has few methods to have code loaded dynamically at runtime, particularly if you don't know what going to be loaded untill the code is actually run.


'''

class siteREs:

	imgFilter = None
	nextPgFilter = None
	startPg = ""
	rootPg = ""
	subDirName = ""

	startNum = 1

	cleanImgNames = 0					#Rename images in order of pages (When images are not uniquely named

	overrideFunc = False

class GGenius(siteREs):

	subDirName = "Girl Genius"

	imgFilter = re.compile("<IMG ALT='Comic' BORDER=0 SRC='(http://www\.girlgeniusonline\.com/ggmain/strips/ggmain\d*?\.\w\w\w|http://www\.girlgeniusonline\.com/ggmain/strips/ggmain[0-9ab]*?\.\w\w\w)' WIDTH='\d*?' HEIGHT='\d*?'/>")
	nextPgFilter = re.compile("<a href='(http://www\.girlgeniusonline\.com/comic\.php\?date=\d*?)'><img border=\d alt='The Next Comic' src='http://www\.girlgeniusonline\.com/!\.images\.page/navbuttons/nextday\.jpg'/></a>", re.DOTALL|re.IGNORECASE)
	startPg = "http://www.girlgeniusonline.com/comic.php?date=20021104"
	rootPg = "http://www.girlgeniusonline.com/"

	startNum = 0

class SMBC(siteREs):

	subDirName = "Saturday Morning Breakfast Cereal"

	imgFilter = re.compile("<img src='((http://zs1\.smbc-comics\.com/comics/|http://www\.smbc-comics\.com/comics/)[0-9-]*?\.gif)'><br>")
	nextPgFilter = re.compile('<area shape="rect" coords="351,21,425,87"\n  href="(/index\.php\?db=comics&id=\d*?)#comic"', re.DOTALL|re.IGNORECASE)
	startPg = "http://www.smbc-comics.com/index.php?db=comics&id=1"
	rootPg = "http://www.smbc-comics.com/"

	startNum = 1

class Outsider(siteREs):

	subDirName = "Outsider"

	imgFilter = re.compile('<img.*?src="(/comics/.*?)" .*?/><br>')
	nextPgFilter = re.compile('<a href="(/d/.*?)"><img border="0px" id="next_day1" alt="Next comic" src="/images/next_day\.gif" style=".*?" />', re.IGNORECASE)
	startPg = "http://well-of-souls.com/outsider/outsider000.html"
	rootPg = "http://sorethumbs.keenspot.com/"

	startNum = 1

class SoreThumbs(siteREs):

	subDirName = "Sore Thumbs"

	imgFilter = re.compile('<img.*?src="(/comics/.*?)" .*?/><br>')
	nextPgFilter = re.compile('<a href="(/d/.*?)"><img border="0px" id="next_day1" alt="Next comic" src="/images/next_day\.gif" style=".*?" />', re.IGNORECASE)
	startPg = "http://sorethumbs.keenspot.com/d/20040308.html"
	rootPg = "http://sorethumbs.keenspot.com/"

	startNum = 1

class GWS(siteREs):

	subDirName = "Girls with Slingshots"

	imgFilter = re.compile('<img src="(images/gws/GWS\d*?\.jpg)"></div></td></tr>')
	nextPgFilter = re.compile('="0"></a><a href="(archive\.php\?today=\d*?&comic=\d*?)" onMouseOver="imgOn.*?img4.*?" onMouseOut="imgOff', re.IGNORECASE)
	startPg = "http://www.daniellecorsetto.com/archive.php?today=1000&comic=1"
	rootPg = "http://www.daniellecorsetto.com/"

	startNum = 700


class somethingPositive(siteREs):

	subDirName = "Something Positive"

	imgFilter = re.compile('<img src="/?((?:arch/|sp).*?)".*?>')
	nextPgFilter = re.compile('<(?!.*?Previous)a.*?href="(.{0,30}?)".*?Next.*?</a>', re.DOTALL|re.IGNORECASE)
	startPg = "http://www.somethingpositive.net/sp08042008.shtml"
	rootPg = "http://www.somethingpositive.net/"

	startNum = 1

	cleanImgNames = 1
	overrideFunc = True

	def getComics(self, upPtr, getHandles, logptr, config):
		print self
		counter = self.startNum
		prevPage = ""

		for link in WCWV2Lists.SpPages:



			upPtr.getPage(link, self, getHandles, logptr, counter, config, prevPage)

			counter = counter + 1

		print("Done downloading Pages")



class dominicDeegan(siteREs):

	subDirName = "Dominic Deegan"

	imgFilter = re.compile('<img src="(comic\.php\?id=\d*)?&amp;save-as=')
	nextPgFilter = re.compile("<a href=\"(view.php.*?)\"><img src='/images/nav/next21.gif' alt='Next'></a>", re.IGNORECASE)
	startPg = "http://www.dominic-deegan.com/view.php?date=2002-05-21"
	rootPg = "http://www.dominic-deegan.com/"

	startNum = 1

	cleanImgNames = 1

class charby(siteREs):

	subDirName = "Charby the Vampirate"

	imgFilter = re.compile("<IMG SRC='http://comics\.drunkduck\.com/(Charby_the_Vampirate/pages/[a-f0-9]*?\.\w\w\w)'")
	nextPgFilter = re.compile("<A HREF=\'/(Charby_the_Vampirate/index\.php\?p=[0-9]*?)' TITLE='The next page!'>")
	startPg = "http://www.drunkduck.com/Charby_the_Vampirate/index.php?p=8839"
	rootPg = "http://www.drunkduck.com/"

	startNum = 1

	cleanImgNames = 1




class pennyArcade(siteREs):

	subDirName = "Penny Arcade"

	imgFilter = re.compile("<div class=\"simplebody\"><img src=\"(/images/\d\d\d\d/\d*?\w?\.\w*?)\" alt=\".*?/></div>")
	nextPgFilter = re.compile("&nbsp;<a href=\"(/comic/\d*?/\d*?/\d*?)/\"><img alt=\"Next\"")
	startPg = "http://www.penny-arcade.com/comic/1998/11/18/"
	rootPg = "http://www.penny-arcade.com"

	startNum = 1




class girlAndFed(siteREs):

	subDirName = "A Girl and her Fed"

	imgFilter = re.compile("<img src=\"http://www\.agirlandherfed\.com(/images/REDO\w*?.jpg)\">")
	nextPgFilter = re.compile("<a href=\"http://www.agirlandherfed.com(/redone/[a-zA-Z0-9\.]*?)\">Next</a>")
	#imgFilter = re.compile("<img src=\"(/images/comics/.*?)\" alt=\"GAHF Comic\" />")
	#nextPgFilter = re.compile("<a  href=\"(/comic/\?\d*?)\" class=\"navigationActive\">Next</a>")
	startPg = "http://www.agirlandherfed.com/redone/redo1intro.shtml"
	rootPg = "http://agirlandherfed.com"

	startNum = 1

	cleanImgNames = 1



class questionableContent(siteREs):

	subDirName = "Questionable Content"

	imgFilter = re.compile("<img id=\"strip\" src=\"./(comics/\d*?.png)\">")
	nextPgFilter = re.compile("<li><a href=\"(view\.php\?comic=\d*?)\">Next</a></li>")
	startPg = "http://www.questionablecontent.net/view.php?comic=1"
	rootPg = "http://www.questionablecontent.net/"

	startNum = 1

	cleanImgNames = 1



class LICD(siteREs):

	subDirName = "Least I Could Do"

	imgFilter = re.compile("<img src=\"(http://archive\.leasticoulddo\.com/strips/\d*?\.gif)\" alt=\"Comic\" \/>")
	nextPgFilter = re.compile("<a href=\"/(comic/\d*?)\" id=\"navbar-next\">Next</a>")
	startPg = "http://www.leasticoulddo.com/comic/20030210"
	rootPg = "http://www.leasticoulddo.com/"

	startNum = 1

	cleanImgNames = 1



class dresdenCodak(siteREs):

	subDirName = "Dresden Codak"

	imgFilter = re.compile("<img src=\"(?!\.\./spacer)(?!\.\./images)(.*?)\" width=\"\d*?\" height=\"\d*?\">")
	nextPgFilter = re.compile("<a href=\"(dc_.{0,10}?\.(html|htm))\"><img src=\"\.\./images/b_next\.gif\" width=\"\d*?\" height=\"\d*?\" border=\"0\"></a>")
	startPg = "http://dresdencodak.com/cartoons/dc_001.htm"
	rootPg = "http://dresdencodak.com/cartoons/"

	startNum = 1

	cleanImgNames = 1




import logmanager

class WebComicWalker(Plugin):

	#Configuration Variables - Loaded when plugin is selected
	capabilities		=	"wScraperDisabled"									#Used for detecting ifany random .py file is a plugin file
	version			=	1.3										#Used for detecting available configuration options
	pluginName		=	"WebcomicWalker"								#Plugin Name variable
	siteName		=	"Webcomic Walker V2"								#Value for populating "Art Site" dropbox
	nameSource		=	[]										#namesource value passed to plugin
	nameSourceEN		=	1										#Determines if Name Source input box is enabled (Boolean, 0-1)
	artistListEN		=	1										#Determines if Artist List input box is enabled (Boolean, 0-1)
	overwriteModes		=	["Check Files", "Overwrite Files"]						#Determines if overwrite mode dropbox is enabled for plugin
	retrievalHandles	=	1										#How may http retrieval handles app wants

	DLFolder		=	"Webcomics"
	logFileName		=	"LogFile.txt"

	#V1.1 variables:

	aNameLabeltxt		=	"Disabled"
	aListLabeltxt		=	"Disabled"
	#V1.2 variables:

	cookieCapable = 0


	#log = logmanager.ThreadLog(retrievalHandles)

	#V1.3 variables:
	nameSource			=	[]				#namesource value passed to plugin (Stuffed at runtime)
	nameSourceEN			=	1				#Determines if Name Source input box is enabled (Boolean, 0-1)


	instructionText		=	\
'''Batch Downloads WebComics

Options:
	Something Positive  *Buggy due to bad website
	Dominic Deegan
	Charby the Vampirate
	Penny Arcade
	A Girl and her Fed
	Questionable Content
	Least I Could Do
	Dresden Codak
	Saturday Morning Breakfast Cereal
	Etc...
See ..\plugins\WebComicWalker.py for information
on how to add aditional web comic pages


'''

	pageFuncList = []

	def __init__(self):

		print "Initing"

		self.nameSource = []
		self.pageFuncList = []

		for item in inspect.getmembers(WCWV2):
			if inspect.isclass(item[1]):
				if issubclass(item[1], siteREs) and item[0] != "siteREs":
					self.pageFuncList.append((item[1].subDirName, item[1]))
					self.nameSource.append(item[1].subDirName)
					#print item[1], item[0]

	def getImage(self, imgURL, getHandles, pageREs, counter, logptr, config):
		if pageREs.cleanImgNames == 1:
			if re.search("http://", imgURL) == None:
				imgURL = pageREs.rootPg+imgURL

			type = re.search("\.(\w\w\w)$", imgURL)
			if type == None:
				type = "jpg"
			else:
				try:
					type = type.group(1)
				except:
					type = "jpg"

			if counter % 1 != 0:
				imgName = ("img%7.1f.%s" % (counter, type)).replace(" ","0")
			else:
				imgName = ("img%05d.%s" % (counter, type))

			imgName = os.path.join(self.DLFolder, pageREs.subDirName, imgName)

		else:
			imgShortName = re.search("(?!.*?/)(.*?)$", imgURL)
			if imgShortName == None:
				self.log.error("Incorrect image name at %s" % imgURL)
				return 0
			#pprint(imgURL)
			#pprint(imgShortName.group(0))
			#pprint(imgShortName)
			imgName = os.path.join(self.DLFolder, pageREs.subDirName, imgShortName.group(1))

			#pprint("Initial imgURL %s" % imgURL)
			if re.search("http://", imgURL) == None:
				imgURL = pageREs.rootPg+imgURL
			#pprint("Processed imgURL %s" % imgURL)
		if pageREs.cleanImgNames == 1:
			self.log.info("Getting : '%s', renaming to: '%s'" % (imgURL, imgName))
		else:
			self.log.info("Getting : '%s', image filename: '%s'" % (imgURL, imgName))



		if os.access(imgName, os.F_OK):
			if os.path.getsize(imgName) < 1000:
				undersize = True
				self.log.info("Existimg Image smaller than 1K - Redownloading")
			else:
				undersize = False
		else:
			undersize = False

		if not config.ovwMode == "Check Files" or not os.access(imgName, os.W_OK) or undersize:
			imgPtr = open(imgName, "wb")
			imgHandle = getHandles.getpage(imgURL, logptr)

			#print "Got Image"

			if imgHandle == "Failed":
				self.log.error("Error : Could not get page 1. Failed")
				self.log.error("%s%s" % (pageREs.rootPg, imgURL))
				self.log.error("%s" % (imgURL))
			else:
				imgPtr.write(imgHandle)
			#print "Wrote Image"
			imgPtr.close()
		else:
			self.log.info("Already have Comic : %s " % imgURL)

	def getPage(self, pageName, pageREs, getHandles, logptr, counter, config, prevPage):
		if re.search("http://", pageName) == None:				#Check if prepending http://$pagename$ is needed
			pageName = pageREs.rootPg+pageName

		self.log.info(pageName)							#Debugging and Info
		currPage = getHandles.getpage(pageName, logptr)				#Get webpage
		if currPage == "Failed":
			self.log.error("Error : Could not get page %s. Failed" % counter)
			self.log.error("%s%s" % (pageREs.rootPg, pageName))
			return 0


		if prevPage == currPage:						#Check if we're looping. Sometimes the "Next page" link points to itself at the most current comic (or even randomly, at badly maintained sites)
			self.log.error("Looping, error in config or art site HTML. Exiting")
			return 0

		imgURL = re.findall(pageREs.imgFilter, currPage)			#Find Image URLs
		print imgURL
		if imgURL == None:							#If none, Error
			self.log.error("Cannot Find Image URL")
			imgURL = None

		elif isList(imgURL):							#If multiple, iterate over them
			self.log.info("Found %s images" % len(imgURL))
			for link in imgURL:
				counter = counter + 0.1
				self.getImage(link, getHandles, pageREs, counter, logptr, config)

		else:
			self.getImage(imgURL, getHandles, pageREs, counter, logptr, config)	#Else, just get the image


		nextPage = re.search(pageREs.nextPgFilter, currPage)
		if nextPage != None:
			nextPage = (nextPage).group(1)
			return nextPage, currPage
		else:
			self.log.error("Cannot Find Next Page URL. Either at end or archive, or there's an error.")
			return None, None



	def main(self, config, getHandles):											 #Main Function Call - Uses other functions

		self.log = logmanager.ThreadLog(self.retrievalHandles)

		#pprint(config)
		#pprint("Artist Name :"+config.aName)
		#pprint("Artist List :"+config.aList)
		#pprint("Name Source :"+config.nSrcMode)
		#pprint("Overwrite Mode :"+config.ovwMode)
		#pprint(getHandles)

		pageREs = None

		for func in self.pageFuncList:
			if config.nSrcMode == func[0]:
				pageREs = func[1]
		if pageREs == None:
			self.log.error("Danger Will Robinson")
			return 0



		#print dir(pageREs)
		#pageREs.config.nSrcMode()
		#return

		#print pageREs.imgFilter
		#print pageREs.nextPgFilter
		#print pageREs.startPg
		#print pageREs.subDirName

#	-	-	-	-	-	-	-	-	-	-	-	-	Directory Management


		self.DLFolder = os.path.join(config.settings.downloadDir, self.DLFolder)

		if not os.access(os.path.join(self.DLFolder), os.W_OK):
				try:
					os.mkdir(os.path.join(self.DLFolder))
					self.log.info("Created folder "+os.path.join(self.DLFolder))
					#pprint("Beginning Download")
				except:
					self.log.error("Cannot Make working directory (%s/. Do you have write Permissions? %s" % (os.path.join(self.DLFolder, pageREs.subDirName), sys.exc_info()[0]))
					return 0
		if not os.access(os.path.join(self.DLFolder, pageREs.subDirName), os.W_OK):
				try:
					os.mkdir(os.path.join(self.DLFolder, pageREs.subDirName))
					self.log.info("Created folder "+os.path.join(self.DLFolder, pageREs.subDirName))
					#pprint("Beginning Download")
				except:
					self.log.error("Cannot Make working directory (%s/. Do you have write Permissions? %s" % (os.path.join(self.DLFolder, pageREs.subDirName), sys.exc_info()[0]))
					return 0


		logFile = os.path.join(self.DLFolder, pageREs.subDirName, self.logFileName)		#Open local Logfile
		#print logFile
		logptr = open(logFile, "a")

		if pageREs.overrideFunc == True:				#If page class wants to override everything, bind the class to an instance, and call the override function
			print "Overridden"
			print "Config ", self, getHandles, logptr, config, pageREs
			getComicOverride = pageREs()
			getComicOverride.getComics(self, getHandles, logptr, config)

		else:

			self.log.info("Getting Page")
	#	-	-	-	-	-	-	-	-	-	-	-	-	Loop over Webpage Archive

			#return
			nextPage = pageREs.startPg
			counter = pageREs.startNum
			prevPage = ""

			while 1:


				nextPage, prevPage = self.getPage(nextPage, pageREs, getHandles, logptr, counter, config, prevPage)

				if nextPage == None and prevPage == None:
					break

				counter = counter + 1

			self.log.info("Done downloading Pages")


		logmanager.clearThreadLog(self.log)

		return 1


